﻿@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    Layout = null;
    var token = Antiforgery.GetAndStoreTokens(Context).RequestToken;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>生产工单</title>
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <style>
        .required::before {
            content: '* ';
            color: red;
        }
    </style>
</head>
<body>
    <form class="layui-form" action="">
        @Html.AntiForgeryToken()
        <div class="layui-form-item">
            <label class="layui-form-label required">工单编码</label>
            <div class="layui-input-inline" style="width: 250px;">
                <input type="text" name="workOrderCode" lay-verify="required" placeholder="请输入工单编码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid">
                <input type="checkbox" lay-filter="autoGenerate" title="自动生成" lay-skin="switch" lay-text="是|否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">工单名称</label>
            <div class="layui-input-inline">
                    <input type="text" name="workOrderName" lay-verify="required" placeholder="请输入工单名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">来源类型</label>
            <div class="layui-input-block">
                <input type="radio" name="sourceType" value="客户订单" title="客户订单" checked>
                <input type="radio" name="sourceType" value="库存备货" title="库存备货">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">单据状态</label>
            <div class="layui-input-block">
                <select name="status" lay-verify="required">
                    <option value="">请选择</option>
                    <option value="0">草稿</option>
                    <option value="1">已确认</option>
                    <option value="2">已完成</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">工单类型</label>
            <div class="layui-input-block">
                <select name="workOrderType" lay-verify="required">
                    <option value="">请选择</option>
                    <option value="自产">自产</option>
                    <option value="外协">外协</option>
                    <option value="外购">外购</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">产品编号</label>
            <div class="layui-input-block">
                <input type="text" name="productCode" lay-verify="required" placeholder="请输入产品编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">产品名称</label>
            <div class="layui-input-block">
                <input type="text" name="productName" lay-verify="required" placeholder="请输入产品名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-block">
                <input type="text" name="specification" placeholder="请输入规格型号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-block">
                <input type="text" name="unit" placeholder="请输入单位" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">工单数量</label>
                <div class="layui-input-inline" style="width: 100px;">
                <input type="number" name="quantity" lay-verify="required|number" placeholder="请输入" autocomplete="off" class="layui-input" min="1">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">需求日期</label>
            <div class="layui-input-inline">
                <input type="text" name="requiredDate" id="date" lay-verify="required|date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">批次号</label>
            <div class="layui-input-block">
                <input type="text" name="batchNumber" placeholder="请输入批次号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remarks" placeholder="请输入备注内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="saveForm">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script src="~/scripts/jquery-3.4.1.min.js"></script>
    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer'], function(){
            var form = layui.form;
            var laydate = layui.laydate;
            var layer = layui.layer;
            var $ = layui.$;

            // 自动生成编号的开关事件
            form.on('switch(autoGenerate)', function(data){
                if(data.elem.checked){
                    // 获取当前日期
                    var now = new Date();
                    var year = now.getFullYear();
                    var month = (now.getMonth() + 1).toString().padStart(2, '0');
                    var day = now.getDate().toString().padStart(2, '0');
                    var hours = now.getHours().toString().padStart(2, '0');
                    var minutes = now.getMinutes().toString().padStart(2, '0');
                    var seconds = now.getSeconds().toString().padStart(2, '0');
                    
                    // 生成工单编号：WO + 年月日时分秒
                    var workOrderCode = 'WO' + year + month + day + hours + minutes + seconds;
                    
                    // 设置到输入框
                    $('input[name="workOrderCode"]').val(workOrderCode);
                } else {
                    // 清空输入框
                    $('input[name="workOrderCode"]').val('');
                }
            });

            // 初始化日期选择器
            laydate.render({
                elem: '#date',
                trigger: 'click'
            });

            // 监听表单提交
            form.on('submit(saveForm)', function(data){
                // 表单验证
                if (!data.field.workOrderCode) {
                    layer.msg('请输入工单编码', {icon: 2});
                    return false;
                }
                if (!data.field.workOrderName) {
                    layer.msg('请输入工单名称', {icon: 2});
                    return false;
                }
                if (!data.field.productCode) {
                    layer.msg('请输入产品编号', {icon: 2});
                    return false;
                }
                if (!data.field.productName) {
                    layer.msg('请输入产品名称', {icon: 2});
                    return false;
                }

                var loadIndex = layer.load(2);
                $.ajax({
                    url: '/ProductionWorkOrderMain/AddProductionWorkOrder',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        workOrderCode: data.field.workOrderCode,
                        workOrderName: data.field.workOrderName,
                        sourceType: data.field.sourceType || "客户订单",
                        documentStatus: 0,  // 默认为草稿状态
                        workOrderType: data.field.workOrderType || "自产",
                        productCode: data.field.productCode,
                        productName: data.field.productName,
                        productModel: data.field.specification || "",
                        unit: data.field.unit || "个",
                        workOrderQuantity: parseInt(data.field.quantity) || 0,
                        demandDate: data.field.requiredDate || new Date().toISOString().split('T')[0],
                        batchNo: data.field.batchNumber || "",
                        remark: data.field.remarks || "",
                        orderCode: ""  // 可选字段，默认为空
                    }),
                    headers: {
                        'RequestVerificationToken': '@token'
                    },
                    success: function(res) {
                        layer.close(loadIndex);
                        if(res.success) {
                            layer.msg('保存成功', {
                                icon: 1,
                                time: 1000
                            }, function(){
                                // 容错处理，避免 window.name 为空时报错
                                if (parent && parent.layer && window.name) {
                                var index = parent.layer.getFrameIndex(window.name);
                                    if (typeof index !== 'undefined' && index !== -1) {
                                parent.layer.close(index);
                                parent.location.reload();
                                    } else {
                                        parent.location.reload();
                                    }
                                } else {
                                    location.reload();
                                }
                            });
                        } else {
                            layer.msg(res.message || '保存失败', {icon: 2});
                        }
                    },
                    error: function() {
                        layer.close(loadIndex);
                        layer.msg('保存失败，请稍后重试', {icon: 2});
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>
